var redundancyEntries = [];
var selectedEntries = [];
var redundancyEntriesPageNumber = 1;
var eachPageNumber1 = 20;
var eachPageNumber2 = 20;
var selectedEntriesPageNumber = 1;

try {
  var tableCurrentPage1 = document.getElementById('tableCurrentPage1');
  var tableTotalPage1 = document.getElementById('tableTotalPage1');
  var tableCurrentPage2 = document.getElementById('tableCurrentPage2');
  var tableTotalPage2 = document.getElementById('tableTotalPage2');

  var goback1 = document.getElementById('goback1').addEventListener('click', ifGoBack1);
  var forward1 = document.getElementById('forward1').addEventListener('click', ifForward1);

  var goback2 = document.getElementById('goback2').addEventListener('click', ifGoBack2);
  var forward2 = document.getElementById('forward2').addEventListener('click', ifForward2);

  var classifyDataDetailTableTmplBody1 = document.getElementById('classifyDataDetailTableTmplBody1');
  var classifyDataDetailTableTmplBody2 = document.getElementById('classifyDataDetailTableTmplBody2');
} catch (err) { console.log(err); }

function clearTable1() {
  classifyDataDetailTableTmplBody1.innerHTML = '';
}

function clearTable2() {
  classifyDataDetailTableTmplBody2.innerHTML = '';
}

function setCurrentPageNumber (key, num) {
  if (key === 'tableCurrentPage1') {
    tableCurrentPage1.innerHTML = num;
  } else {
    tableCurrentPage2.innerHTML = num;
  }
}

function setTotalPageNumber(key, num) {
  if (key === 'tableTotalPage1') {
    tableTotalPage1.innerHTML = num;
  } else {
    tableTotalPage2.innerHTML = num;
  }
}

// if go back1
function ifGoBack1() {
  if (redundancyEntriesPageNumber > 1) {
    redundancyEntriesPageNumber --;
    // 首先，全部设置为不能显示
    redundancyEntries.forEach((item, index) => {
      item.isShow = 0;
    });
    redundancyEntries.forEach((item, index) => {
      if (item && index >= (redundancyEntriesPageNumber - 1) * eachPageNumber1 && index < redundancyEntriesPageNumber * eachPageNumber1 ) {
        item.isShow = 1;
      } else {
        item.isShow = 0;
      }
    });
    clearTable1();
    setCurrentPageNumber('tableCurrentPage1', redundancyEntriesPageNumber);
    $("#classifyDataDetailTableTmpl1").tmpl(redundancyEntries).appendTo("#classifyDataDetailTableTmplBody1");
    // $("#classifyDataDetailTableTmpl2").tmpl(selectedEntries).appendTo("#one-table3");
  } else {
    // 不能向前翻页
  }
}

// if go back2
function ifGoBack2() {
  if (selectedEntriesPageNumber > 1) {
    selectedEntriesPageNumber --;
    // 首先，全部设置为不能显示
    selectedEntries.forEach((item, index) => {
      item.isShow = 0;
    });
    selectedEntries.forEach((item, index) => {
      if (item && index >= (selectedEntriesPageNumber - 1) * eachPageNumber2 && index < selectedEntriesPageNumber * eachPageNumber2 ) {
        item.isShow = 1;
      } else {
        item.isShow = 0;
      }
    });
    clearTable2();
    setCurrentPageNumber('tableCurrentPage2', selectedEntriesPageNumber);
    // $("#classifyDataDetailTableTmpl1").tmpl(redundancyEntries).appendTo("#one-table2");
    $("#classifyDataDetailTableTmpl2").tmpl(selectedEntries).appendTo("#classifyDataDetailTableTmplBody2");
  } else {
    // 不能向前翻页
  }
}

// if forward1
function ifForward1() {
  if (redundancyEntriesPageNumber < (redundancyEntries.length / eachPageNumber1)) {
    redundancyEntriesPageNumber ++;
    // 首先，全部设置为不能显示
    redundancyEntries.forEach((item, index) => {
      item.isShow = 0;
    });
    redundancyEntries.forEach((item, index) => {
      if (item && index >= (redundancyEntriesPageNumber - 1) * eachPageNumber1 && index < redundancyEntriesPageNumber * eachPageNumber1 ) {
        item.isShow = 1;
      } else {
        item.isShow = 0;
      }
    });
    clearTable1();
    setCurrentPageNumber('tableCurrentPage1', redundancyEntriesPageNumber);
    $("#classifyDataDetailTableTmpl1").tmpl(redundancyEntries).appendTo("#classifyDataDetailTableTmplBody1");
    // $("#classifyDataDetailTableTmpl2").tmpl(selectedEntries).appendTo("#one-table3");
  } else {
    // 不能向前翻页
  }
}

// if forward1
function ifForward2() {
  if (selectedEntriesPageNumber < (selectedEntries.length / eachPageNumber2)) {
    selectedEntriesPageNumber ++;
    // 首先，全部设置为不能显示
    selectedEntries.forEach((item, index) => {
      item.isShow = 0;
    });
    selectedEntries.forEach((item, index) => {
      if (item && index >= (selectedEntriesPageNumber - 1) * eachPageNumber2 && index < selectedEntriesPageNumber * eachPageNumber2 ) {
        item.isShow = 1;
      } else {
        item.isShow = 0;
      }
    });
    clearTable2();
    setCurrentPageNumber('tableCurrentPage2', selectedEntriesPageNumber);
    // $("#classifyDataDetailTableTmpl1").tmpl(redundancyEntries).appendTo("#one-table2");
    $("#classifyDataDetailTableTmpl2").tmpl(selectedEntries).appendTo("#classifyDataDetailTableTmplBody2");
  } else {
    // 不能向前翻页
  }
}


//弹出层中间过程
function open_status_window(text){//显示状态
  $(".doing>img").attr("src","images/big.gif");
  set_text(text);
  set_status_window_position();
  $(".zyc").show();
  $(".doing").show();
  $(window.document.body).css("overflow","hidden");
}

function close_status_window(relay_time){//关闭  参数：延迟时间 毫秒
  setTimeout(function(){
    $(".zyc").hide();
    $(".doing").hide();
    $(window.document.body).css("overflow","auto");
  },relay_time);
}

function set_status_window_position(){//设置状态框位置
  var wind = $(".doing");
  var client = $(window);
  var sc_top = $(window.document).scrollTop();
  var self_width = parseInt(wind.css("width"));
  var self_height = parseInt(wind.css("height"));
  var client_width = client.width();
  var client_height = client.height();
  var real_left = (client_width - self_width) / 2;
  var real_top = (client_height - self_height) / 2;
  wind.css("left",real_left+"px");
  wind.css("top",real_top+sc_top+"px");
}
function set_text(text){//为窗口设置文本消息
  $(".doing>p").empty();
  $(".doing>p").text(text);
  set_status_window_position();
}
//弹出层中间过程

//首页的用户名下拉列表
var list_status = false;
function show_user_list(){//显示列表
  if(!list_status){
    var user = $("#user");
    var list = $("#user_list");
    var user_width = parseInt(user.css("width"));
    list.css("width",user_width+"px");
    set_user_list_position();
    list.show();
    list_status = true;
  }else{
    hide_user_list();
    list_status = false;
  }
}
function set_user_list_position(){//设置位置
  var user = $("#user");
  var list = $("#user_list");
  var user_height = parseInt(user.css("height"));
  list.css("top",user_height+"px");
}
function hide_user_list(){//隐藏列表
  var list = $("#user_list");
  list.hide();
}
function add_style_user(){

}
function remove_style_user(){}


//首页的用户名下拉列表
//点击链接后定位到图片的位置
function find_img() {
  var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
  //var scroll_top = $(document.body).scrollTop;
  if (document.documentElement.scrollTop) {
    $(document.body).scrollTop(-640);
  } else if (document.body.scrollTop) {
  }
  $(document.body).scrollTop(640);
}
  function getWheelValue(e) {//获取鼠标滚动delta值
    var evt = window.event || e //equalize event object
    var delta = evt.detail ? evt.detail * (-120) : evt.wheelDelta //check for detail first so Opera uses that instead of wheelDelta
    return delta;
  }

//下载文件
  function down_file() {
    $.ajax({
      url: "/temp_table/downLoadData",
      type: "get",
      dataType: "json",
      data: "",
      cache: false,
      success: function (data) {
        if (data.result == "SUCCESS") {

        } else {
          event.preventDefault();
        }
      },
      error: function (msg) {
        my_notice(msg);
        event.preventDefault();
      }
    });
  }

//下载文件

//想后台传文件路径以及描述
  function send_selected_data(name, desc) {//用户选择的name，和desc，描述
    clearTable1();
    clearTable2();
    $.ajax({
      url: "/LoadData/selectData",
      type: "post",
      data: {
        dataName: name,
        description: desc,
        path: getData1().path
      },
      dataType: "json",
      cache: false,
      success: function (data) {
        if (data.result === "SUCCESS") {
          close_status_window(500);
          redundancyEntries = data.message.redundancyEntries;
          selectedEntries = data.message.selectedEntries;
          setCurrentPageNumber('tableCurrentPage1', 1);
          setCurrentPageNumber('tableCurrentPage2', 1);
          setTotalPageNumber('tableTotalPage1', cacuTotalPageNumber(redundancyEntries.length, eachPageNumber1)),
          selectedEntries = data.message.selectedEntries;
          setTotalPageNumber('tableTotalPage2', cacuTotalPageNumber(selectedEntries.length, eachPageNumber2)),
          redundancyEntries.forEach((item, index) => {
            if (index < eachPageNumber1) {
              item.isShow = 1; // display
            } else {
              item.isShow = 0; // none
            }
          });
          selectedEntries.forEach((item, index) => {
            if (index < eachPageNumber2) {
              item.isShow = 1; // display
            } else {
              item.isShow = 0; // none
            }
          });
          $("#classifyDataDetailTableTmpl1").tmpl(redundancyEntries).appendTo("#classifyDataDetailTableTmplBody1");//tmpl模板往table里面插入数据
          $("#classifyDataDetailTableTmpl2").tmpl(selectedEntries).appendTo("#classifyDataDetailTableTmplBody2");//tmpl模板往table里面插入数据
        } else {
          close_status_window(500);
        }
      },
      error: function (msg) {
        my_notice("Error...");
      }
    });
  }

//想后台传文件路径以及描述
//查询log日志
  function search_logs() {//查询logs文件
    $.ajax({
      url: "/Logger/getLogger",
      type: "post",
      dataType: "json",
      cache: false,
      data: "",
      success: function (data) {

        if (data.result === "SUCCESS") {
          document.getElementById("operation-right").innerHTML = transfer_logs(data.message);
          close_status_window(500);
          has_loged = true;
        } else {
          set_text("Error...");
          close_status_window(500);
        }
      },
      error: function (msg) {
        set_text("Connecting error...");
        close_status_window(500);
      }
    });
    open_status_window("Loading...");
  }

  function send_reset_message() {//发送重置logs信息
    $("#my-reset").modal("hide");
    $.ajax({
      url: "/Logger/resetLogger",
      type: "post",
      dataType: "json",
      cache: false,
      data: "",
      success: function (data) {
        if (data.result == "SUCCESS") {
          close_status_window(500);
          search_logs();
        } else {
          set_text("Error...");
          close_status_window(500);
        }
      },
      error: function (msg) {
        set_text("Error...");
        close_status_window(500);
      }
    });
    open_status_window("Loading...");

}
//查询log日志
function transfer_logs(str){//logs日志转换
  var str_temp = str.replace(/@/g,"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
  return str_temp;
}

// 把分析文件的数据保存在
function storeFilesData(key, data) {
  window.localStorage.setItem(key, JSON.parse(data));
}

// 清空文件数据
function clearFilesData(key) {
  window.localStorage.removeItem(key);
}

// 获取localStorage中Data1的值
function getData1() {
  return JSON.parse($.cookie('data1'));
}

// 计算总页数
function cacuTotalPageNumber(rows, eachPageNumber) {
  return Math.ceil(rows / eachPageNumber);
}